package com.zuoye.Dao;

import com.zuoye.Opjo.Class;
import com.zuoye.UBtil.DbUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;

/**
 * @version v1.0
 * @Description
 * @Author admin
 * @date 2021/7/30  16:55
 * @Copyright
 */
public class ClaDaoIntImpl implements ClassDaoInterface {

    @Override
    public int insert(ArrayList<Class> list) {
        String sql = "insert into cla(name,num,createdate) values (?,?,?)";
        try (Connection connection = DbUtil.getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
            int i = 0;
            Iterator<Class> iterator = list.iterator();
            while (iterator.hasNext()) {
                Class aClass = iterator.next();
                preparedStatement.setString(1, aClass.getName());
                preparedStatement.setInt(2, aClass.getNum());
                preparedStatement.setString(3, aClass.getCreatedate());
                i = preparedStatement.executeUpdate();
            }
            return i;

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    @Override
    public int delete(int claId) {
        String sql = "delete from cla where id = ?";
        try (Connection connection = DbUtil.getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
            preparedStatement.setInt(1, claId);
            int i = preparedStatement.executeUpdate();
            return i;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    @Override
    public int update(Class cla) {
        String sql = "update cla set name=?,num=?,createdate=? where id=?";
        try (Connection connection = DbUtil.getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
            int i = 0;
            preparedStatement.setString(1, cla.getName());
            preparedStatement.setInt(2, cla.getNum());
            preparedStatement.setString(3, cla.getCreatedate());
            preparedStatement.setInt(4, cla.getId());
            i = preparedStatement.executeUpdate();
            System.out.println("i = " + i);
            return i;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    @Override
    public ArrayList<Class> query() {
        final ArrayList<Class> list = new ArrayList<>();
        try (final Connection connection = DbUtil.getConnection();
             final PreparedStatement preparedStatement = connection.prepareStatement("select * from cla");
             final ResultSet resultSet = preparedStatement.executeQuery()) {
            while (resultSet.next()) {
                final Class aClass = new Class();
                aClass.setName(resultSet.getString("name"));
                aClass.setNum(resultSet.getInt("num"));
                aClass.setCreatedate(resultSet.getString("createdate"));
                list.add(aClass);
                System.out.println("aClass = " + aClass);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int transaction(Connection connection, ArrayList<Class> list) {
        String sql = "insert into cla(name,num,createdate) values (?, ?, ?)";
        String querySql = "select LAST_INSERT_ID()";
        try (final PreparedStatement preparedStatement1 = connection.prepareStatement(sql);
             final PreparedStatement preparedStatement2 = connection.prepareStatement(querySql);) {
            Iterator<Class> iterator = list.iterator();
            int claId = 0;
            while (iterator.hasNext()) {
                Class cla = iterator.next();
                preparedStatement1.setString(1, cla.getName());
                preparedStatement1.setInt(2, cla.getNum());
                preparedStatement1.setString(3, cla.getCreatedate());
                preparedStatement1.executeUpdate();
                final ResultSet resultSet = preparedStatement2.executeQuery();
                while (resultSet.next()) {
                    claId = resultSet.getInt(1);
                }
                return claId;
            }
        } catch (SQLException throwables) {
            throw new RuntimeException("插入班级异常");
        }
        return 0;
    }
}
